package com.example.androidexp22

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.content.Intent
import android.os.Bundle
import android.os.Parcel
import android.os.Parcelable
import android.view.View
import android.widget.Button
import android.widget.CalendarView
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import java.util.*


class MainActivity : AppCompatActivity() {
    data class Event(
        val date: String,
        val calories: Int
    ) : Parcelable {
        constructor(parcel: Parcel) : this(
            parcel.readString() ?: "",
            parcel.readInt()
        )

        override fun writeToParcel(parcel: Parcel, flags: Int) {
            parcel.writeString(date)
            parcel.writeInt(calories)
        }

        override fun describeContents(): Int {
            return 0
        }

        companion object CREATOR : Parcelable.Creator<Event> {
            override fun createFromParcel(parcel: Parcel): Event {
                return Event(parcel)
            }

            override fun newArray(size: Int): Array<Event?> {
                return arrayOfNulls(size)
            }
        }
    }
    private lateinit var calendarView: CalendarView
    private lateinit var btnAddEvent: Button
    private lateinit var btnGoToBlankActivity: Button
    private lateinit var btnGoToDateChoose: Button
    private lateinit var  etCalories: EditText
    private lateinit var datePickerDialog: DatePickerDialog // 声明DatePickerDialog
    private lateinit var CaloriesLayout: LinearLayout
    private lateinit var btnConfirmCalories:Button
    private var latestSelectedDateStr: String? = null

    @SuppressLint("MissingInflatedId")
    private fun initializeViews() {
        calendarView = findViewById(R.id.calendarView)
        btnAddEvent = findViewById(R.id.btnAddEvent)
        btnGoToBlankActivity = findViewById(R.id.btnGoToBlankActivity)
        btnGoToDateChoose = findViewById(R.id.btnGoToDateChoose)
        etCalories = findViewById(R.id.etCalories)
        CaloriesLayout = findViewById(R.id.CaloriesLayout)
        btnConfirmCalories = findViewById(R.id.btnConfirmCalories)
    }
    private fun initDatePicker(): DatePickerDialog {
        val c = Calendar.getInstance()
        return DatePickerDialog(this, { _, year, monthOfYear, dayOfMonth ->
            val selectedDate = "$dayOfMonth/${monthOfYear + 1}/$year"
            println("Selected Date: $selectedDate")
            latestSelectedDateStr = selectedDate
            CaloriesLayout.visibility = View.VISIBLE
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH))
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // 初始化视图
        initializeViews()
        // 修改DatePickerDialog的回调处理
        datePickerDialog = initDatePicker()
        CaloriesLayout.visibility = View.GONE // 初始隐藏卡路里输入界面
        // 添加活动按钮的点击事件处理
        btnAddEvent.setOnClickListener {
            datePickerDialog.show()
        }
        btnGoToBlankActivity.setOnClickListener {
            val intent = Intent(this, EventDetailsActivity::class.java)
            startActivity(intent)
        }
        btnConfirmCalories.setOnClickListener {
            val enteredCalories = etCalories.text.toString().trim()
            if (enteredCalories.isEmpty()) {
                Toast.makeText(this, "请输入卡路里含量", Toast.LENGTH_SHORT).show()
            } else {
                val calories = enteredCalories.toIntOrNull()
                if (calories == null || calories <= 0) {
                    Toast.makeText(this, "请输入有效的卡路里数值", Toast.LENGTH_SHORT).show()
                } else if (latestSelectedDateStr != null) {
                    val intent = Intent(this, DateChoose::class.java)
                    intent.putExtra("selected_date", latestSelectedDateStr)
                    intent.putExtra("selected_calories", calories)
                    startActivity(intent)
                } else {
                    Toast.makeText(this, "请先选择一个日期", Toast.LENGTH_SHORT).show()
                }
            }
        }
        btnGoToDateChoose.setOnClickListener {
            val intent = Intent(this, DateChoose::class.java)
            startActivity(intent)
//            if (selectedDates.isNotEmpty()) {
//                val intent = Intent(this, DateChoose::class.java).apply {
//                    putStringArrayListExtra("selected_dates", ArrayList(selectedDates.map { it.date }))
//                    putIntegerArrayListExtra("selected_calories", ArrayList(selectedDates.map { it.calories }))
//                }
//                startActivity(intent)
//            } else {
//                Toast.makeText(this, "请选择至少一个日期", Toast.LENGTH_SHORT).show()
//            }
        }

    }
}

